﻿using UnityEngine;
using System.Collections;
using System;

public class HttpGet : MonoBehaviour {
    private static HttpGet _instance = null;

    public static HttpGet Instance{
        get
        {
            if(_instance == null)
            {
                GameObject go = new GameObject();
                go.name = "Http";
                _instance = go.AddComponent<HttpGet>();

            }
            return _instance;
        }    
    }
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void Get(string url,Action<string> onSuccess,Action<string> onfail)
    {
        StartCoroutine(RequestGet(url, onSuccess, onfail));
    }

    IEnumerator RequestGet(string url, Action<string> onSuccess, Action<string> onfail)
    {
        WWW www = new WWW(url);
        yield return www;
        if (!string.IsNullOrEmpty(www.text))
        {
            onSuccess(www.text);
        }
        if (!string.IsNullOrEmpty(www.error))
        {
            onfail(www.error);
        }
    }
}
